package com.mida.app.knw.service.impl;

import com.mida.app.knw.service.DetailCodingSystemService;
import com.mida.app.knw.service.KnwConceptualService;
import com.mida.dms.knw.api.KnwConceptualApi;
import com.mida.dms.knw.entity.KnwCodingConceptualDomain;
import com.mida.dms.knw.entity.vo.KccdPage;
import com.mida.dms.knw.entity.vo.KnwCodingConceptualDomainPageVo;
import com.mida.dms.knw.entity.vo.KnwConceptualDomainVo;
import com.mida.dms.knw.entity.vo.KnwConceptualVo;
import com.mida.dms.knw.vo.Page;
import com.mida.dms.knw.vo.PageResultVo;
import com.mida.dms.mdm.pojo.auto.MsdDetailCodingSystem;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: KnwConceptualServiceImpl
 * @Description: TODO
 * @Author: wang xiao le
 * @Date: 2021/6/21 20:30
 **/
@Service
@Slf4j
@Transactional
public class KnwConceptualServiceImpl implements KnwConceptualService {

    @Autowired
    private KnwConceptualApi knwConceptualApi;
    @Autowired
    private DetailCodingSystemService detailCodingSystemService;

    /**
     * 5、概念分页查询
     *
     * @param currentPage 当前页
     * @param pageSize    每页数量
     * @param searChData  搜索关键词
     * @param conceptType 概念类型
     * @return
     */
    @Override
    public ListResult<PageResultVo<KnwConceptualVo>> findPage(Long currentPage, Long pageSize, String searChData, Long conceptType) {

        log.info("开始分页查询");
        StopWatch st = StopWatch.createStarted();

        ListResult<PageResultVo<KnwConceptualVo>> page1 = null;
        try {
            page1 = knwConceptualApi.findPage(currentPage, pageSize, searChData, conceptType);
            List<KnwConceptualVo> knwConceptualVolist = page1.getData().getList();
            //补充状态,和概念类型
            if (!CollectionUtils.isEmpty(knwConceptualVolist)) {
                List<Long> statusSoids = knwConceptualVolist.stream().map(KnwConceptualVo::getStatus).distinct().collect(Collectors.toList());
                Map<Long, String> dataSetNameMap = detailCodingSystemService.getDataSetNameMap(statusSoids);
                List<Long> conceptTypeList = knwConceptualVolist.stream().map(KnwConceptualVo::getConceptType).distinct().collect(Collectors.toList());
                Map<Long, String> conceptTypeNameMap = detailCodingSystemService.getDataSetNameMap(conceptTypeList);
                knwConceptualVolist.forEach((knwConceptualVo) -> {
                    knwConceptualVo.setStatusname(dataSetNameMap.get(knwConceptualVo.getStatus()));
                    knwConceptualVo.setConceptTypeName(conceptTypeNameMap.get(knwConceptualVo.getConceptType()));
                });
            }
        } catch (Exception e) {
            log.error("分页查编码体系发送异常", e);
        } finally {
            st.stop();
            log.info("本次分页查询耗时:" + st.getTime() + "ms");
        }
        return page1;
    }


    /**
     * 8、概念域绑定编码体系查询
     *
     * @param conceptualSoid 概念soid
     */
    @Override
    public ListResult<List<KnwConceptualDomainVo>> getKnwConceptualDomain(long conceptualSoid) {
        log.info("开始获取一个概念soid" + conceptualSoid);
        StopWatch st = StopWatch.createStarted();
        ListResult<List<KnwConceptualDomainVo>> knwConceptualDomain = new ListResult<List<KnwConceptualDomainVo>>();
//        List<KnwConceptualDomainVo> listVo = new ArrayList<KnwConceptualDomainVo>();
        try {
            knwConceptualDomain = knwConceptualApi.getKnwConceptualDomain(conceptualSoid);
            List<KnwConceptualDomainVo> knwConceptualDomainVoList = knwConceptualDomain.getData();
            //补充状态,和概念类型
            if (!CollectionUtils.isEmpty(knwConceptualDomainVoList)) {

                List<Long> conceptDomainTypeSoidList = knwConceptualDomainVoList
                        .stream()
                        .map(KnwConceptualDomainVo::getConceptDomainTypeSoid)
                        .distinct()
                        .collect(Collectors.toList());
                Map<Long, String> conceptDomainTypeMap = detailCodingSystemService.getDataSetNameMap(conceptDomainTypeSoidList);
                knwConceptualDomainVoList.forEach(knwConceptualDomainVo ->
                        knwConceptualDomainVo.setConceptDomainTypeName(conceptDomainTypeMap.get(knwConceptualDomainVo.getConceptDomainTypeSoid())));

            }
        } catch (Exception e) {
            log.error("根据概念soid，查询概念域相关数据业务处理异常", e);
        } finally {
            st.stop();
            log.info("本次查询耗时:" + st.getTime() + "ms");
        }
        return knwConceptualDomain;
    }

    /**
     * 9、概念域查询所有编码体系
     *
     * @param domainSoid 概念域soid
     * @return
     */
    @Override
    public ListResult<List<KnwCodingConceptualDomainPageVo>> getCodingDomainAll(Long domainSoid) {

        log.info("开始概念域查询所有编码体系,查询条件:");
        StopWatch st = StopWatch.createStarted();
        ListResult<List<KnwCodingConceptualDomainPageVo>> codingDomainAll = new ListResult<>();
        try {
            codingDomainAll = knwConceptualApi.getCodingDomainAll(domainSoid);
            List<KnwCodingConceptualDomainPageVo> knwCodingConceptualDomainPageVoList = codingDomainAll.getData();
            //补充编码体系name
            if (!CollectionUtils.isEmpty(knwCodingConceptualDomainPageVoList)) {
                List<Long> derailCodingSystemSoidList = knwCodingConceptualDomainPageVoList
                        .stream()
                        .map(KnwCodingConceptualDomainPageVo::getDerailCodingSystemSoid)
                        .distinct()
                        .collect(Collectors.toList());
                //TODO 补充编码体系name和标识符
                Map<Long, MsdDetailCodingSystem> detailCodingNameMap = detailCodingSystemService.getDetailCodingMap(derailCodingSystemSoidList);
                knwCodingConceptualDomainPageVoList.forEach(knwCodingConceptualDomainPageVo -> {
                    MsdDetailCodingSystem msdDetailCodingSystem = detailCodingNameMap.get(knwCodingConceptualDomainPageVo.getDerailCodingSystemSoid());
                    knwCodingConceptualDomainPageVo.setDerailCodingSystemName(msdDetailCodingSystem.getCnName());
                    knwCodingConceptualDomainPageVo.setCodingSystemIdentifier(msdDetailCodingSystem.getCodingSystemIdentifier());
                });
            }

        } catch (Exception e) {

            log.error("概念域查询所有编码体系发送异常", e);
        } finally {
            st.stop();
            log.info("本次概念域查询所有编码体系耗时:" + st.getTime() + "ms");

        }
        return codingDomainAll;
    }

    /**
     * 10、概念域分页查询
     * (未完成)
     *
     * @param domainSoid  概念域soid
     * @param currentPage 当前页
     * @param pageSize    每页数量
     * @return
     */
    @Override
    public ListResult<PageResultVo<KnwCodingConceptualDomainPageVo>> getCodingDomainPage(Long domainSoid, Long currentPage, Long pageSize) {

        log.info("开始分页查询,查询条件:" + domainSoid);
        StopWatch st = StopWatch.createStarted();
        ListResult result = new ListResult();
        List<KnwCodingConceptualDomain> list = null;
        ListResult<PageResultVo<KnwCodingConceptualDomainPageVo>> codingDomainPage = new ListResult<>();
        try {
            codingDomainPage = knwConceptualApi.getCodingDomainPage(domainSoid, currentPage, pageSize);
            List<KnwCodingConceptualDomainPageVo> knwCodingConceptualDomainPageVoList = codingDomainPage.getData().getList();
            if (!CollectionUtils.isEmpty(knwCodingConceptualDomainPageVoList)) {
                List<Long> derailCodingSystemSoidList = knwCodingConceptualDomainPageVoList
                        .stream()
                        .map(KnwCodingConceptualDomainPageVo::getDerailCodingSystemSoid)
                        .distinct()
                        .collect(Collectors.toList());
                //TODO 补充编码体系name和标识符
                Map<Long, MsdDetailCodingSystem> detailCodingNameMap = detailCodingSystemService.getDetailCodingMap(derailCodingSystemSoidList);
                knwCodingConceptualDomainPageVoList.forEach(knwCodingConceptualDomainPageVo -> {
                    MsdDetailCodingSystem msdDetailCodingSystem = detailCodingNameMap.get(knwCodingConceptualDomainPageVo.getDerailCodingSystemSoid());
                    knwCodingConceptualDomainPageVo.setDerailCodingSystemName(msdDetailCodingSystem.getCnName());
                    knwCodingConceptualDomainPageVo.setCodingSystemIdentifier(msdDetailCodingSystem.getCodingSystemIdentifier());
                });
            }
        } catch (Exception e) {
            log.error("分页查询概念域列表业务处理异常", e);
        } finally {
            st.stop();
            log.info("本次分页查询耗时:" + st.getTime() + "ms");
        }
        return codingDomainPage;
    }
}
